package com.bowlong.third.redis.lang;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Vector;

import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.Jedis;

public class JedisList {

	final Jedis jedis;
	final String pkey;

	public JedisList(Jedis jedis, String pkey) {
		this.jedis = jedis;
		this.pkey = pkey;
	}

	public int size() {
		return this.jedis.llen(pkey).intValue();
	}

	public boolean isEmpty() {
		return size() <= 0;
	}

	public List<String> getAll() {
		List<String> r2 = new Vector<>();
		List<String> datas = this.jedis.lrange(pkey, 0, -1);
		for (String tex : datas) {
			r2.add(tex);
		}
		return r2;
	}

	public Iterator<String> iterator() {
		List<String> list = getAll();
		return list.iterator();
	}

	public Object[] toArray() {
		List<String> list = getAll();
		return list.toArray();
	}

	public <T> T[] toArray(T[] a) {
		List<String> list = getAll();
		return list.toArray(a);
	}

	public boolean add(String e) {
		this.jedis.rpush(pkey, e);
		return true;
	}

	public boolean remove(String tex) {
		this.jedis.lrem(pkey, 1, tex);
		return false;
	}

	public boolean containsAll(Collection<String> c) {
		List<String> ctexs = new Vector<>();
		for (String tex : c) {
			ctexs.add(tex);
		}
		List<String> texs = this.jedis.lrange(pkey, 0, -1);
		return texs.containsAll(ctexs);
	}

	public boolean addAll(Collection<String> c) {
		for (String tex : c) {
			this.jedis.rpush(pkey, tex);
		}
		return true;
	}

	public boolean addAll(int index, Collection<String> c) {
		int index2 = index;
		for (String tex : c) {
			String pivot = String.valueOf(index2);
			this.jedis.linsert(pkey, LIST_POSITION.AFTER, pivot, tex);
			index2++;
		}
		return true;
	}

	public boolean removeAll(Collection<String> c) {
		for (String tex : c) {
			this.jedis.lrem(pkey, 1, tex);
		}
		return true;
	}

	public boolean retainAll(Collection<String> c) {
		List<String> ctexs = new Vector<>();
		for (String tex : c) {
			ctexs.add(tex);
		}
		List<String> texs = this.jedis.lrange(pkey, 0, -1);
		return texs.retainAll(ctexs);
	}

	public void clear() {
		this.jedis.del(pkey);
	}

	public String get(int index) {
		String tex = this.jedis.lindex(pkey, index);
		if (tex == null || tex.isEmpty())
			return null;

		return tex;
	}

	public String set(int index, String tex) {
		this.jedis.lset(pkey, index, tex);
		return tex;
	}

	public void add(int index, String tex) {
		String pivot = String.valueOf(index);
		this.jedis.linsert(pkey, LIST_POSITION.AFTER, pivot, tex);
	}

	public String remove(int index) {
		String tex = this.jedis.lindex(pkey, index);
		if (tex == null || tex.isEmpty())
			return null;
		this.jedis.ltrim(pkey, index, index + 1);
		return tex;
	}

	public int indexOf(String tex) {
		List<String> texs = this.jedis.lrange(pkey, 0, -1);
		return texs.indexOf(tex);
	}

	public int lastIndexOf(String tex) {
		List<String> texs = this.jedis.lrange(pkey, 0, -1);
		return texs.lastIndexOf(tex);
	}

	public ListIterator<String> listIterator() {
		List<String> list = getAll();
		return list.listIterator();
	}

	public ListIterator<String> listIterator(int index) {
		List<String> list = getAll();
		return list.listIterator(index);
	}

	public List<String> subList(int fromIndex, int toIndex) {
		List<String> r2 = new Vector<>();
		List<String> datas = this.jedis.lrange(pkey, fromIndex, toIndex);
		for (String tex : datas) {
			r2.add(tex);
		}
		return r2;
	}

}
